<template>
  <el-dialog v-model="dialogVisible" title="原因卡片" width="500" :before-close="closeDia">
    <div>
      <el-input
        v-model="value"
        maxlength="30"
        style="width: 240px"
        placeholder="Please input"
        show-word-limit
        rows="10"
        type="textarea"
      />
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="confirmClick" :loading="loading"> 确定 </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { inject, ref } from 'vue'
import { ElMessage, ElNotification } from 'element-plus'
import request from '@/util/request'

const dialogVisible = ref(false)
const value = ref('')
const loading = ref(false)

const formNoPass = ref({})

const open = (item, val) => {
  dialogVisible.value = true
  console.log('接收的数据', item)
  formNoPass.value.contentId = item.id
  formNoPass.value.contentTypeId = item.typeId
  formNoPass.value.userId = item.userId
  formNoPass.value.contentStatus = item.status
  formNoPass.value.id = val
  formNoPass.value.status = judgOperate(val)
}

const closeDia = () => {
  dialogVisible.value = false
  value.value = []
}

const { updateData } = inject('reload')

const confirmClick = () => {
  loading.value = true

  formNoPass.value.msg = value.value

  console.log('提交的数据', formNoPass.value)
  request
    .post('/admin/examine/ispass', formNoPass.value)
    .then((res) => {
      console.log('---', res)
      ElMessage.success('操作成功')
      setTimeout(() => {
        dialogVisible.value = false
        loading.value = false
        updateData()
      }, 500)
    })
    .catch((err) => {
      if (err.response) {
        console.error(err.message)
        // ElMessage.error('出错了', err.message)
        ElNotification({
          title: 'Error',
          message: err.message,
          type: 'error'
        })
      } else {
        ElNotification({
          title: 'Error',
          message: '网络错误',
          type: 'error'
        })
      }

      setTimeout(() => {
        dialogVisible.value = false
        loading.value = false
        updateData()
      }, 500)
    })
}

const judgOperate = (val) => {
  if (val === 1) {
    return '通过'
  } else if (val === 2) {
    return '不通过'
  } else if (val === 3) {
    return '重审'
  }
}

defineExpose({
  open
})
</script>
